﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace Lion.WeiXin.Core.Data
{
   public class CategoryDAO:ICategory
    {
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int AID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from T_Category");
            strSql.Append(" where AID=@AID");
            SqlParameter[] parameters = {
					new SqlParameter("@AID", SqlDbType.Int,4)
			};
            parameters[0].Value = AID;

            return SqlHelper.Exists(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string code)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from T_Category");
            strSql.Append(" where Code=@Code");
            SqlParameter[] parameters = {
					new SqlParameter("@Code", SqlDbType.VarChar,12)
			};
            parameters[0].Value = code;

            return SqlHelper.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Models.CategoryModel model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_Category(");
            strSql.Append("Code,ParentCode,Name,DirName,Image,Summary,Url,Content,TemplateID,IsShow,Sort)");
            strSql.Append(" values (");
            strSql.Append("@Code,@ParentCode,@Name,@DirName,@Image,@Summary,@Url,@Content,@TemplateID,@IsShow,@Sort)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@Code", SqlDbType.VarChar,12),
					new SqlParameter("@ParentCode", SqlDbType.VarChar,12),
					new SqlParameter("@Name", SqlDbType.VarChar,50),
					new SqlParameter("@DirName", SqlDbType.VarChar,50),
					new SqlParameter("@Image", SqlDbType.VarChar,250),
					new SqlParameter("@Summary", SqlDbType.NVarChar,2000),
					new SqlParameter("@Url", SqlDbType.VarChar,250),
					new SqlParameter("@Content", SqlDbType.Text),
					new SqlParameter("@TemplateID", SqlDbType.Int,4),
                    new SqlParameter("@IsShow", SqlDbType.Int,4),
                    new SqlParameter("@Sort", SqlDbType.Int,4)};
            parameters[0].Value = model.Code;
            parameters[1].Value = model.ParentCode;
            parameters[2].Value = model.Name;
            parameters[3].Value = model.DirName;
            parameters[4].Value = model.Image;
            parameters[5].Value = model.Summary;
            parameters[6].Value = model.Url;
            parameters[7].Value = model.Content;
            parameters[8].Value = model.TemplateID;
            parameters[9].Value = model.IsShow;
            parameters[10].Value = model.Sort;

            object obj = SqlHelper.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Models.CategoryModel model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_Category set ");
            strSql.Append("Code=@Code,");
            strSql.Append("ParentCode=@ParentCode,");
            strSql.Append("Name=@Name,");
            strSql.Append("DirName=@DirName,");
            strSql.Append("Image=@Image,");
            strSql.Append("Summary=@Summary,");
            strSql.Append("Url=@Url,");
            strSql.Append("Content=@Content,");
            strSql.Append("TemplateID=@TemplateID,");
            strSql.Append("IsShow=@IsShow,");
            strSql.Append("Sort=@Sort");
            strSql.Append(" where AID=@AID");
            SqlParameter[] parameters = {
					new SqlParameter("@Code", SqlDbType.VarChar,12),
					new SqlParameter("@ParentCode", SqlDbType.VarChar,12),
					new SqlParameter("@Name", SqlDbType.VarChar,50),
					new SqlParameter("@DirName", SqlDbType.VarChar,50),
					new SqlParameter("@Image", SqlDbType.VarChar,250),
					new SqlParameter("@Summary", SqlDbType.NVarChar,2000),
					new SqlParameter("@Url", SqlDbType.VarChar,250),
					new SqlParameter("@Content", SqlDbType.Text),
					new SqlParameter("@TemplateID", SqlDbType.Int,4),
                    new SqlParameter("@IsShow", SqlDbType.Int,4),
                    new SqlParameter("@Sort", SqlDbType.Int,4),
					new SqlParameter("@AID", SqlDbType.Int,4)};
            parameters[0].Value = model.Code;
            parameters[1].Value = model.ParentCode;
            parameters[2].Value = model.Name;
            parameters[3].Value = model.DirName;
            parameters[4].Value = model.Image;
            parameters[5].Value = model.Summary;
            parameters[6].Value = model.Url;
            parameters[7].Value = model.Content;
            parameters[8].Value = model.TemplateID;
            parameters[9].Value = model.IsShow;
            parameters[10].Value = model.AID;
            parameters[11].Value = model.Sort;

            int rows = SqlHelper.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int AID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from T_Category ");
            strSql.Append(" where AID=@AID");
            SqlParameter[] parameters = {
					new SqlParameter("@AID", SqlDbType.Int,4)
			};
            parameters[0].Value = AID;

            int rows = SqlHelper.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string AIDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from T_Category ");
            strSql.Append(" where AID in (" + AIDlist + ")  ");
            int rows = SqlHelper.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Models.CategoryModel GetModel(int AID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 AID,Code,ParentCode,Name,DirName,Image,Summary,Url,Content,TemplateID,IsShow,Sort from T_Category ");
            strSql.Append(" where AID=@AID");
            SqlParameter[] parameters = {
					new SqlParameter("@AID", SqlDbType.Int,4)
			};
            parameters[0].Value = AID;

            Models.CategoryModel model = new Models.CategoryModel();
            DataSet ds = SqlHelper.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return ConvertToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Models.CategoryModel GetModel(string code)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 AID,Code,ParentCode,Name,DirName,Image,Summary,Url,Content,TemplateID,IsShow,Sort from T_Category ");
            strSql.Append(" where Code=@Code");
            SqlParameter[] parameters = {
					new SqlParameter("@Code", SqlDbType.VarChar,12)
			};
            parameters[0].Value = code;

            Models.CategoryModel model = new Models.CategoryModel();
            DataSet ds = SqlHelper.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return ConvertToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return new Models.CategoryModel();
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Models.CategoryModel ConvertToModel(DataRow row)
        {
            Models.CategoryModel model = new Models.CategoryModel();
            if (row != null)
            {
                if (row["AID"] != null && row["AID"].ToString() != "")
                {
                    model.AID = int.Parse(row["AID"].ToString());
                }
                if (row["Code"] != null)
                {
                    model.Code = row["Code"].ToString();
                }
                if (row["ParentCode"] != null)
                {
                    model.ParentCode = row["ParentCode"].ToString();
                }
                if (row["Name"] != null)
                {
                    model.Name = row["Name"].ToString();
                }
                if (row["DirName"] != null)
                {
                    model.DirName = row["DirName"].ToString();
                }
                if (row["Image"] != null)
                {
                    model.Image = row["Image"].ToString();
                }
                if (row["Summary"] != null)
                {
                    model.Summary = row["Summary"].ToString();
                }
                if (row["Url"] != null)
                {
                    model.Url = row["Url"].ToString();
                }
                if (row["Content"] != null)
                {
                    model.Content = row["Content"].ToString();
                }
                if (row["TemplateID"] != null && row["TemplateID"].ToString() != "")
                {
                    model.TemplateID = int.Parse(row["TemplateID"].ToString());
                }
                if (row["IsShow"] != null && row["IsShow"].ToString() != "")
                {
                    model.IsShow = int.Parse(row["IsShow"].ToString());
                }
                if (row["Sort"] != null && row["Sort"].ToString() != "")
                {
                    model.Sort = int.Parse(row["Sort"].ToString());
                }
            }
            return model;
        }

        protected List<Models.CategoryModel> ConvertToList(DataSet ds)
        {
            List<Models.CategoryModel> list = new List<Models.CategoryModel>();
            foreach (DataRow row in ds.Tables[0].Rows)
            {
                Models.CategoryModel model = ConvertToModel(row);
                list.Add(model);
            }
            return list;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<Models.CategoryModel> GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select AID,Code,ParentCode,Name,DirName,Image,Summary,Url,Content,TemplateID,IsShow,Sort ");
            strSql.Append(" FROM T_Category ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            DataSet ds = SqlHelper.Query(strSql.ToString());
            return ConvertToList(ds);
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public List<Models.CategoryModel> GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" AID,Code,ParentCode,Name,DirName,Image,Summary,Url,Content,TemplateID,IsShow,Sort ");
            strSql.Append(" FROM T_Category ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            DataSet ds = SqlHelper.Query(strSql.ToString());
            return ConvertToList(ds);
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM T_Category ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = SqlHelper.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public List<Models.CategoryModel> GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.AID desc");
            }
            strSql.Append(")AS Row, T.*  from T_Category T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            DataSet ds = SqlHelper.Query(strSql.ToString());
            return ConvertToList(ds);
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "T_Category";
            parameters[1].Value = "AID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return SqlHelper.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

    }
}
